import request from '../../utils/request';
import { ProcessDefinitionVO } from './model';

export interface Task {
  id: string;
  name: string;
}

export interface ProcessInstanceVO {
  id: number;
  name: string;
  processDefinitionId: string;
  category: string;
  result: number;
  tasks: Task[];
  fields: string[];
  status: number;
  remark: string;
  businessKey: string;
  createTime: string;
  endTime: string;
  processDefinition?: ProcessDefinitionVO;
}

export interface User {
  id: number;
  nickname: string;
  avatar: string;
}

export interface ApprovalTaskInfo {
  id: number;
  ownerUser: User;
  assigneeUser: User;
  status: number;
  reason: string;
  signPicUrl: string;
}

export interface ApprovalNodeInfo {
  id: number;
  name: string;
  nodeType: string;
  candidateStrategy?: string;
  status: number;
  startTime?: Date;
  endTime?: Date;
  candidateUsers?: User[];
  tasks: ApprovalTaskInfo[];
}

// BPM 流程实例管理 API
export const ProcessInstanceApi = {
  // 查询我的流程实例分页
  getProcessInstanceMyPage: async (params: any) => {
    return await request.get('/bpm/process-instance/my-page', { params });
  },

  // 查询管理流程实例分页
  getProcessInstanceManagerPage: async (params: any) => {
    return await request.get('/bpm/process-instance/manager-page', { params });
  },

  // 创建流程实例
  createProcessInstance: async (data: any) => {
    return await request.post('/bpm/process-instance/create', data);
  },

  // 发起人取消流程实例
  cancelProcessInstanceByStartUser: async (id: number, reason: string) => {
    return await request.delete('/bpm/process-instance/cancel-by-start-user', {
      data: { id, reason }
    });
  },

  // 管理员取消流程实例
  cancelProcessInstanceByAdmin: async (id: number, reason: string) => {
    return await request.delete('/bpm/process-instance/cancel-by-admin', {
      data: { id, reason }
    });
  },

  // 查询流程实例详情
  getProcessInstance: async (id: string) => {
    return await request.get(`/bpm/process-instance/get?id=${id}`);
  },

  // 查询抄送流程实例分页
  getProcessInstanceCopyPage: async (params: any) => {
    return await request.get('/bpm/process-instance/copy/page', { params });
  },

  // 获取审批详情
  getApprovalDetail: async (params: any) => {
    return await request.get('/bpm/process-instance/get-approval-detail', { params });
  },

  // 获取表单字段权限
  getFormFieldsPermission: async (params: any) => {
    return await request.get('/bpm/process-instance/get-form-fields-permission', { params });
  },

  // 获取流程实例的 BPMN 模型视图
  getProcessInstanceBpmnModelView: async (id: string) => {
    return await request.get(`/bpm/process-instance/get-bpmn-model-view?id=${id}`);
  }
};